#include "InGame.h"
#include "../algorithms/flocking.h"
#include "../vehicle.h"

InGame::InGame() 
{

}

/*-------------------------------------------------------------------------------------------------------------*/
InGame::~InGame() 
{

}

/*-------------------------------------------------------------------------------------------------------------*/
bool InGame::Initialize(int iLevel) 
{
	m_iStage = GAME_STAGE_NONE;

	m_pPeon = new Peon(0);
	m_pGoldMine = new GoldMine(1);
	m_pTownHall = new TownHall(2);
	
	if (!Level::Initialize(iLevel))
	{
		return false;
	}

	if(!m_pPeon->Initialize())
	{
		return false;
	}

	if(!m_pGoldMine->Initialize())
	{
		return false;
	}

	if(!m_pTownHall->Initialize())
	{
		return false;
	}

	m_pPeon->SetPos(300.0f , -200.0f);
	m_pGoldMine->SetPos(-300.0f , -200.0f);
	m_pTownHall->SetPos(200.0f, 200.0f);

	m_pPeon->GetFSM()->ChangeState(WalkState::Instance());

	// Vehicles

	for (int a=0; a<10; ++a)
	{

		//determine a random starting position
		Vector2D SpawnPos = Vector2D(RandInRange(1, -1) * 400.0 , RandInRange(1, -1)* 300.0);

		Vehicle* pVehicle = new Vehicle(this, RandFloat()*(3.14)*2, Vector2D(0,0), 2.0 * 200.0, 0.5, 3.14, 1.0);

		if(!pVehicle->Initialize())
		{
			return false;
		}

		pVehicle->SetPos(SpawnPos.x, SpawnPos.y);

		pVehicle->FlockingInstance()->FlockingOn();

		m_Vehicles.push_back(pVehicle);
	}

	return true;

}

/*-------------------------------------------------------------------------------------------------------------*/
bool InGame::Shutdown() 
{
	delete m_pPeon;
	delete m_pGoldMine;
	delete m_pTownHall;

	for (unsigned int a=0; a<m_Vehicles.size(); ++a)
	{
		delete m_Vehicles[a];
	}

	return true;
}

/*-------------------------------------------------------------------------------------------------------------*/
void InGame::Update(float fDt) 
{
	// If we press Esc we exit
	if (KeyDown(DIK_ESCAPE))
	{
		m_iStage = GAME_STAGE_EXIT;
	}

	m_pPeon->Update(fDt);
	m_pGoldMine->Update(fDt);
	m_pTownHall->Update(fDt);

	for (unsigned int a=0; a<m_Vehicles.size(); ++a)
	{
		m_Vehicles[a]->Update(fDt);
	}
}

/*-------------------------------------------------------------------------------------------------------------*/
void InGame::Draw()
{
	Level::Draw();
	m_pGoldMine->Draw();
	m_pTownHall->Draw();
	m_pPeon->Draw();
	for (unsigned int a=0; a<m_Vehicles.size(); ++a)
	{
		m_Vehicles[a]->Draw();
	}
}